package com.aliwo.basic;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * java 基础 try with resource.
 * try()可以定义多个资源，他们的关闭顺序是最后在try里面定义的最先关闭
 *
 * @author Xu Yayuan
 * @date 2022/3/26
 */
public class DemoTryResource {
    public static void main(String[] args) {
        test();
    }

    public static void test() {
        try (
                FileInputStream fis = new FileInputStream("D:\\test.txt");
                BufferedInputStream bis = new BufferedInputStream(fis);
                FileOutputStream fos = new FileOutputStream("D:\\copy.txt");
                BufferedOutputStream bos = new BufferedOutputStream(fos);
        ) {
            int size;
            byte[] buf = new byte[1024];
            while ((size = bis.read(buf)) != -1) {
                bos.write(buf, 0, size);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
